package com.ruoyi.basedocument.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basedocument.domain.BomAsscategory;
import com.ruoyi.util.importRunnable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basedocument.domain.BomCategory;
import com.ruoyi.basedocument.service.IBomCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 类别Controller
 * 
 * @author ruoyi
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/bomcostestimation/basedocument/category")
public class BomCategoryController extends BaseController
{
    @Autowired
    private IBomCategoryService bomCategoryService;
    @Autowired
    private importRunnable importRunnable;

    /**
     * 查询类别列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomCategory bomCategory)
    {
        startPage();
        List<BomCategory> list = bomCategoryService.selectBomCategoryList(bomCategory);
        return getDataTable(list);
    }

    /**
     * 导出类别列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:export')")
    @Log(title = "类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomCategory bomCategory)
    {
        List<BomCategory> list = bomCategoryService.selectBomCategoryList(bomCategory);
        ExcelUtil<BomCategory> util = new ExcelUtil<BomCategory>(BomCategory.class);
        util.exportExcel(response, list, "类别数据");
    }

    /**
     * 获取类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bomCategoryService.selectBomCategoryById(id));
    }

    /**
     * 新增类别
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:add')")
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomCategory bomCategory)
    {
        return toAjax(bomCategoryService.insertBomCategory(bomCategory));
    }

    /**
     * 修改类别
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:edit')")
    @Log(title = "类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomCategory bomCategory)
    {
        return toAjax(bomCategoryService.updateBomCategory(bomCategory));
    }

    /**
     * 删除类别
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:category:remove')")
    @Log(title = "类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bomCategoryService.deleteBomCategoryByIds(ids));
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void downTemplate(HttpServletResponse response) {
        ExcelUtil<BomCategory> util = new ExcelUtil<>(BomCategory.class);
        util.importTemplateExcel(response, "类别");
    }

    /**
     * 导入数据
     */
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestBody List<BomCategory> list) {
        importRunnable.removeParam();
        importRunnable.setBomCategoryList(list);
        Thread thread = new Thread(importRunnable);
        thread.start();
        return toAjax(1);
//        for (int i = 0; i < list.size(); i++) {
//            BomCategory bomCategory = list.get(i);
//            BomCategory bomCategory1 = new BomCategory();
//            bomCategory1.setNumber(bomCategory.getNumber());
//            List<BomCategory> bomCategories = bomCategoryService.selectBomCategoryList2(bomCategory1);
//            if(bomCategories!=null&&bomCategories.size()!=0){
//                for (int i1 = 0; i1 < bomCategories.size(); i1++) {
//                    bomCategoryService.deleteBomCategoryById(bomCategories.get(i1).getId());
//                }
//            }
//            bomCategoryService.insertBomCategory(bomCategory);
//        }
    }
}
